package com.leetcode.algorithm.y22.m08.w4;

/**
 * 1493. 删掉一个元素以后全为 1 的最长子数组
 * 
 * https://leetcode.cn/problems/longest-subarray-of-1s-after-deleting-one-element/
 * 
 * @author jie.deng
 *
 */
class Question1493Solution01 {

	// 滑动窗口
	public int longestSubarray(int[] nums) {
		int len = nums.length;
		int longest = -1;
		int left = 0;
		int invalidCnt = 0;
		int right = 0;
		for (; right < len; right++) {
			if (nums[right] != 1) {
				invalidCnt++;
			}
			while (invalidCnt > 1) {
				if (nums[left] != 1) {
					invalidCnt--;
				}
				left++;
			}
			longest = Math.max(longest, right - left + 1);
		}

		return longest - 1;
	}

}